class SectionsController < ApplicationController
  load_and_authorize_resource :nested => Collection

  before_filter :find_collection, :only=>[:index, :new, :create]
  cache_sweeper :document_sweeper, :only => [:update, :destroy]
  cache_sweeper :collection_sweeper, :only => [:create, :update, :destory]
  
  caches_page :show
  cache_sweeper :section_sweeper, :only => [:create, :update, :destroy]
  
  # GET /sections
  # GET /sections.xml
  def index
    @sections = @collection.root_sections.paginate(:page=>params[:page])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @sections }
    end
  end
  
  # GET /sections/1/search
  # GET /sections/1/search.xml
  def search
    @section = Section.find(params[:id])
    @search_results = @section.search(params[:query])

    respond_to do |format|
      format.html { render :template=>'documents/search_results'}
      format.xml  { render :xml => @search_results.docs }
    end
  end

  # GET /sections/1
  # GET /sections/1.xml
  def show
    @section = Section.find(params[:id])
    @subsections = @section.children.paginate(:page=>params[:page], :order=>'section_no')
    @documents = @section.documents.paginate(:page=>params[:page], :order=>'document_no')

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @section }
    end
  end

#  # GET /sections/new
#  # GET /sections/new.xml
#  def new
#    @section = Section.new
#
#    respond_to do |format|
#      format.html # new.html.erb
#      format.xml  { render :xml => @section }
#    end
#  end

  # GET /sections/1/edit
  def edit
    @section = Section.find(params[:id])
  end

#  # POST /sections
#  # POST /sections.xml
#  def create
#    @section = Section.new(params[:section])
#
#    respond_to do |format|
#      if @section.save
#        flash[:notice] = 'Section was successfully created.'
#        format.html { redirect_to(@section) }
#        format.xml  { render :xml => @section, :status => :created, :location => @section }
#      else
#        format.html { render :action => "new" }
#        format.xml  { render :xml => @section.errors, :status => :unprocessable_entity }
#      end
#    end
#  end

  # PUT /sections/1
  # PUT /sections/1.xml
  def update
    @section = Section.find(params[:id])

    respond_to do |format|
      if @section.update_attributes(params[:section])
        flash[:notice] = 'Section was successfully updated.'
        format.html { redirect_to(section_url(@section)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @section.errors, :status => :unprocessable_entity }
      end
    end
  end

#  # DELETE /sections/1
#  # DELETE /sections/1.xml
#  def destroy
#    @section = Section.find(params[:id])
#    @section.destroy
#
#    respond_to do |format|
#      format.html { redirect_to(sections_url) }
#      format.xml  { head :ok }
#    end
#  end

protected
  def find_collection
    @collection = Collection.find(params[:collection_id])
  end
end
